using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;
using Guard = Microsoft.Practices.Unity.Utility.Guard;
using Microsoft.Practices.Unity.ObjectBuilder;

namespace UnityContrib.CanonicalObjectExtension
{
    public class CanonicalObject : CanonicalObjectBase
    {
        public CanonicalObject()
        {
        }

        #region UnityContainerExtension Implementation
        protected override void Initialize()
        {
            ConfigureDefaultExtension();
        }
        #endregion

        #region ConfigureDefaultExtension
        public override void ConfigureDefaultExtension()
        {
            // add default strategies 
            Context.Strategies.AddNew<CanonicalObjectTypeStrategy>(UnityBuildStage.PreCreation);
        }
        #endregion 

        public override ICanonicalObject RegisterPartialType(Type fromType, Type toType, string canonicalName,string typeName, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
        {
            if (toType != null && !fromType.IsGenericType && !toType.IsGenericType)
            {
                Guard.TypeIsAssignable(fromType, toType, "fromType");
            }
            Guard.ArgumentNotNullOrEmpty(canonicalName, "canonicalName");

            CanonicalTypeKey key = new CanonicalTypeKey(toType, canonicalName);
            PartialObjectPolicy policy = new PartialObjectPolicy(fromType, toType, canonicalName, typeName);
            Context.Policies.Set<IPartialObjectPolicy>(policy, key);
            return this;
        }
        public override ICanonicalObject RemovePartialType(Type fromType, Type toType, string canonicalName, string typeName)
        {
            // TODO 
            return this;
        }
     
    }
}
